package com.gollum.system.service.impl;

import com.gollum.common.context.PageContextHolder;
import com.gollum.common.domain.response.TableSupport;
import com.gollum.common.exception.BusinessException;
import com.gollum.common.utils.BeanUtils;
import com.gollum.common.utils.StringUtils;
import com.gollum.jpa.common.Specifications;
import com.gollum.jpa.domain.Page;
import com.gollum.jpa.service.JpaServerImpl;
import com.gollum.system.domain.dao.SysLogininforDao;
import com.gollum.system.domain.model.SysLogininfor;
import com.gollum.system.domain.request.SysLogininforRequest;
import com.gollum.system.domain.response.SysLogininforResponse;
import com.gollum.system.service.SysLogininforService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;


/**
 * 登录信息service 实现
 * @author Herther
 * @version 1.0.0
 * @createTime 2022年09月04日 23:07:00
 */
@Service
@Slf4j
public class SysLogininforServiceImpl extends JpaServerImpl<SysLogininfor,Long> implements SysLogininforService {

    @Autowired
    protected SysLogininforDao logininforDao;



    @Override
    public TableSupport<SysLogininforResponse> listPage(SysLogininforRequest sysLogininforRequest) {
        //tips: 需要controller构建分页才能拿到PageContextHolder 才能拿到分页的参数
        Page<SysLogininfor> page = PageContextHolder.get().toPage();
        //查询
        page = findAllWithPage(page,getParams(sysLogininforRequest));

        return TableSupport.toTableResponse(page, BeanUtils.copyToList(page.getRecords(), SysLogininforResponse.class));
    }

    /**
     * 构建列表查询参数
     *
     * @param sysLogininforRequest 前端请求的参数
     * @return jpa参数查询标准
     * @auther: Herther
     * @since 1.0.0
     * @date: 2022/9/17 21:08
     */
    private Specification<SysLogininfor> getParams(SysLogininforRequest sysLogininforRequest){
        //构建查询条件
         Specification<SysLogininfor> spec = Specifications.<SysLogininfor>and()
                .like(StringUtils.isNotEmpty(sysLogininforRequest.getUsername())
                        , SysLogininfor::getUsername, sysLogininforRequest.getUsername())

                .eq(StringUtils.isNotNull(sysLogininforRequest.getId())
                        , SysLogininfor::getId, sysLogininforRequest.getId())

                .like(StringUtils.isNotEmpty(sysLogininforRequest.getIpLocation())
                        , SysLogininfor::getIpLocation, sysLogininforRequest.getIpLocation())

                .eq(StringUtils.isNotEmpty(sysLogininforRequest.getIpAddress())
                        , SysLogininfor::getIpAddress)

                .eq(StringUtils.isNotEmpty(sysLogininforRequest.getStatus())
                        , SysLogininfor::getStatus,sysLogininforRequest.getStatus())

                 .between(getBetweenCondition(sysLogininforRequest.getBeginTime()
                                 ,sysLogininforRequest.getEndTime())
                         ,SysLogininfor::getLoginTime
                         , sysLogininforRequest.getBeginTime(),sysLogininforRequest.getEndTime())
                .build();
         return spec;
    }


    @Override
    public SysLogininforResponse getDetailById(Long id) {
        SysLogininfor sysLogininfor = this.getById(id);
        if (Objects.isNull(sysLogininfor)){
            throw new BusinessException("查询不存在");
        }
        SysLogininforResponse sysLogininforResponse = new SysLogininforResponse();
        BeanUtils.copyBeanProp(sysLogininfor, sysLogininforResponse);
        return sysLogininforResponse;
    }

    @Override
    public void removeByIds(List<Long> ids) {
        try{
            this.removeAllByIds(ids);
        }catch (Exception e){
            throw new BusinessException("删除的对象不存在");
        }
    }


}
